package jdbcDemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JdbcUtil2 {
    private static String USERNAME;
    private static String PASSWORD;
    private static String DRIVER;
    private static String URL;

    // 定义数据库连接
    private Connection connection;
    // 定义sql语句的执行对象
    private PreparedStatement pstmt;
    // 定义查询返回的结果集合
    private ResultSet resultSet;


    public JdbcUtil2() {
        super();
    }

    static {
        loadConfig();
    }

    /**
     * 加载数据库配置信息
     */
    public static void loadConfig() {
        USERNAME = "root";
        PASSWORD = "123456";
        DRIVER = "com.mysql.cj.jdbc.Driver";
        URL = "jdbc:mysql://106.12.33.28/java_bighomework?useSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC";
    }



    /**
     * 获取数据库连接
     * @return
     */
    public Connection getConnection() {
        try {
            Class.forName(DRIVER);
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (Exception e) {
            throw new RuntimeException("获取Connection出现异常！", e);
        }
        return connection;
    }

}
